<?php
/*
 * @Author: KingMin 
 * @Date: 2021-07-13 22:34:59 
 * @Last Modified by:   KingMin 
 * @Last Modified time: 2021-07-13 22:34:59 
 * @The authors website: https://www.kingmin.cn  
 */
/**
 * 配置查询及设置
 * @copyright (c) KINGMIN All Rights Reserved
 */

class dbBackup {
    /**
     * 构造方法
     * SystemDatabackupServices constructor.
     */
    
    /**
     * 获取数据库列表
     * @return array
     * @throws \think\db\exception\BindParamException
     */
    public static function getDataList($pages){
        $DB=Database::getInstance();
        @$isAll=$pages['isAll'];
        $output = [
			'pages'=>[
			    'isAll'=>$isAll?true:false,
    			'recordCount' => 0,     //满足条件的总记录数
    		    'pageSize' => 10,        //每页大小，即每页最多可以显示的记录数量
    		    'pageCount' => 0,       //总页数
    		    'pno' => 1,          //当前数据所在页号
			 ],
			'list' => [] ,         //当前页中的数据
	    ];
	    $bySql="";
	    $isfym="";$wSql=" WHERE `TABLE_SCHEMA` = '".DB_NAME."' ";
	    if(@$pages['pno']){
		  $output['pages']['pno'] = intval($pages['pno']);
		}
		if(@$pages['pageSize']){
		  $output['pages']['pageSize']= intval($pages['pageSize']);
		}
	    $pno = $output['pages']['pno'];$pgs =$output['pages']['pageSize'];
	    
	    $totalSql = " SELECT COUNT(*) as total  FROM information_schema.TABLES $wSql ;";
		$res = $DB->fetch_array($DB->query($totalSql));
		$output['pages']['recordCount'] = intval($res['total']);
		//计算总页数
		$output['pages']['pageCount'] = ceil($output['pages']['recordCount']/$output['pages']['pageSize']);
		//获取指定页中的数据
		$start = ($output['pages']['pno']-1)*$output['pages']['pageSize'];
		$count = $output['pages']['pageSize'];
		if(!$output['pages']['isAll']){
			$isfym="LIMIT $start,$count";
	    }
	    
	    $sql = "SELECT * FROM information_schema.TABLES $wSql  $isfym;";
        $logs = $DB->query($sql);
        $output['sql']=$sql;
        $data = array();
        $output['nrs']=0;
        foreach ($logs as $key => $row) {
            $tbl=$row['TABLE_NAME'];
            $gain= $row['DATA_FREE'];
            $output['nrs'] =$output['nrs'] +$row['TABLE_ROWS'];
            $total_size = $row[ "DATA_LENGTH" ] +  $row[ "INDEX_LENGTH" ];
            $row['kb']=format_size($total_size);
            $row['gain']=format_size($gain);
            $data[]=$row;
        }
        $output['list']=$data;
        return $output;
        
    }
    // 获取数据表表结构
    public static function getRead($tablename=""){
        if(!$tablename){
            return false;
        }
        $DB = Database::getInstance();
        $sql=" select * from information_schema.columns where table_name = '" . $tablename . "' and table_schema = '" .DB_NAME. "' ";
        $alltables=$DB->query($sql);
        
        $list=[];
        while($row = $DB->fetch_array($alltables)) {
            $row['EXTRA'] = ($row['EXTRA'] == 'auto_increment' ? '是' : ' ');
            $list[]=$row;
        }
        $count = count($list);
        $data['list']=$list;
        $data['count']=$count;
        return $data;
    }
    /**
     * 优化表
     *
     * @param array|string $tables
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public static function optimize($tables) {
        if ($tables) {
            $db = Database::getInstance();
             $list = $db->query("OPTIMIZE TABLE {$tables}");
            if (!$list) {
                return ("data sheet'{$tables}'Repair mistakes please try again!");
            }
            return $list;
        } else {
            return ("Please specify the table to be repaired!");
        }
    }

    /**
     * 修复表
     *
     * @param string|null $tables
     * @return array
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public static function repair($tables = null) {
        if ($tables) {
            $db = Database::getInstance();
            $list = $db->query("REPAIR TABLE {$tables}");
            if ($list) {
                return $list;
            } else {
                return ("data sheet'{$tables}'Repair mistakes please try again!");
            }
        } else {
            return ("Please specify the table to be repaired!");
        }
    }
}